<template>
  <!-- 注册页面的整体盒子 -->
  <div class="login-containet">
    <!-- //注册的盒子 -->
    <div class="login-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册表单区域 -->
      <div class="btu-box2">
        <el-form ref="form" :model="form" :rules="rulesObj">
          <el-form-item prop="username">
            <el-input
              placeholder="请输入用户名"
              v-model="form.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              placeholder="请输入密码"
              v-model="form.password"
            ></el-input>
          </el-form-item>
          <el-form-item prop="repassword">
            <el-input
              type="password"
              placeholder="请再次输入密码"
              v-model="form.repassword"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button class="btn-reg" type="primary" @click="onSubmit"
              >注册</el-button
            >
            <el-link type="info" :underline="false" @click="goLogin"
              >去登陆</el-link
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { registerApi } from '@/api/index'
export default {
  data: function () {
    // 确认密码的验证规则
    const samePwd = (rule, value, callback) => {
      if (value !== this.form.password) {
        callback(new Error('两次密码不一致'))
      } else {
        callback()
      }
    }
    return {
      form: {
        username: '', // 用户名
        password: '', // 密码
        repassword: '' // 确认密码
      },
      rulesObj: {
        // 验证用户名
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            trigger: 'blur',
            pattern: /^[a-zA-z0-9]{1,10}$/,
            message: '用户名必须是1-10的大小写字母数字'
          }
        ],
        // 验证密码
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            trigger: 'blur',
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15的非空字符'
          }
        ],
        // 验证密码是否相同
        repassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: samePwd, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // js兜底校验elemnt的表单验证方法
    onSubmit () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          // valid是表单验证的结国
          // 通过校验
          // 调取接口获取响应数据
          const { data: res } = await registerApi(this.form)
          // 注册失败
          if (res.code !== 0) return this.$message.error(res.message)
          else {
            // 注册成功
            this.$message.success(res.message)
            // 跳转到登录页
            this.$router.push('/login')
          }
          // 没有通过验证，阻止表单默认行为
        } else {
          return false
        }
      })
    },
    // 点击去登录
    goLogin () {
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="less" scoped>
.login-containet {
  background: url(@/assets/images/login_bg.jpg) center;
  background-size: cover;
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;

  .login-box {
    width: 400px;
    height: 335px;
    background-color: white;
    border-radius: 3px;
    .title-box {
      height: 60px;
      background: url(@/assets/images/login_title.png) center no-repeat;
      padding: 8px 0;
      box-sizing: border-box;
    }
    .btu-box2 {
      margin: 0 30px;
      .btn-reg {
        width: 100%;
      }
    }
  }
}
</style>
